package com.algorithms;

import java.util.Arrays;

/**
 * 直接插入排序
 * Created by zhuangqi on 17-6-21.
 */
public class InsertDirectSort {

    public static void main(String[] args) {
        int[] waitSort = {3, 8, 7, 6, 10, 9};
        System.out.println("插入排序前:" + Arrays.toString(waitSort));
        waitSort = insertSort(waitSort);
        System.out.println("插入排序后：" + Arrays.toString(waitSort));
    }

    public static int[] insertSort(int[] waitSort){
        //遍历数组
        for (int i = 0; i < waitSort.length; i++) {
            int left = waitSort[i];
            for (int j=i+1; j < waitSort.length; j++){
                int right = waitSort[j];
                if (right<left){
                    int swap = waitSort[i];
                    waitSort[i] = waitSort[j];
                    waitSort[j] = swap;
                }
            }
        }
        return waitSort;
    }
}
